/*******************************************************************************
 * Copyright (c) 2011 TXT e-solutions SpA
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * This work was performed within the IoT_at_Work Project
 * and partially funded by the European Commission's
 * 7th Framework Programme under the research area ICT-2009.1.3
 * Internet of Things and enterprise environments.
 *
 *
 * Authors:
 *     Cristoforo Seccia (TXT e-solutions SpA)
 *
 * Contributors:
 *      Domenico Rotondi (TXT e-solutions SpA)
 *******************************************************************************/
package it.txt.access.capability.wizard.panel;

import it.txt.access.capability.factory.values.CapabilityAction;
import it.txt.access.capability.wizard.panel.controller.ControllerPanelAccessTypeCapability;
import it.txt.access.capability.wizard.panel.exception.PanelAccessTypeCapabilityException;
import java.awt.Frame;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Cristoforo Seccia (TXT e-solutions SpA)
 */
public class PanelAccessTypeCapability extends javax.swing.JPanel {

    private ControllerPanelAccessTypeCapability controller;
    private DateChooserDialog notBeforeDateDialog = null;
    private DateChooserDialog notAfterDateDialog = null;
    private Frame myContainer;

    /** Creates new form WelcomePanel */
    public PanelAccessTypeCapability(Frame container, ControllerPanelAccessTypeCapability cpatc) {
        initComponents();
        checkNotBeforeDateDocument();
        checkNotBeforeTimeDocument();
        checkNotAfterDateDocument();
        checkNotAfterTimeDocument();
        checkDelegateToAuthorize();
        checkAssigneeDocument();
        controller = cpatc;
        myContainer = container;
        notBeforeDateDialog = new DateChooserDialog(myContainer, true);
        notBeforeDateDialog.setLocale(Locale.UK);
        notAfterDateDialog = new DateChooserDialog(myContainer, true);
        notAfterDateDialog.setLocale(Locale.UK);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        centerPanel = new javax.swing.JPanel();
        centerTitlePanel = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        welcomeLabel = new javax.swing.JLabel();
        policyIssuerLabel = new javax.swing.JLabel();
        policyAssigneeLabel = new javax.swing.JLabel();
        policyResourceIDLabel = new javax.swing.JLabel();
        policyAssigneeText = new javax.swing.JTextField();
        policyResourceIDText = new javax.swing.JTextField();
        policyRevocationSrvLabel = new javax.swing.JLabel();
        policyRevocationSrvComboBox = new javax.swing.JComboBox();
        policyAccessRightsPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        policyAccessRightsTable = new javax.swing.JTable();
        policyValidityConditionPanel = new javax.swing.JPanel();
        poliocyValidityConditionNotBeforeLabel = new javax.swing.JLabel();
        poliocyValidityConditionNotAfterLabel = new javax.swing.JLabel();
        poliocyValidityConditionNotBeforeDateButton = new javax.swing.JButton();
        poliocyValidityConditionNotAfterDateButton = new javax.swing.JButton();
        policyValidityConditionNotBeforeDateText = new javax.swing.JFormattedTextField();
        policyValidityConditionNotBeforeTimeText = new javax.swing.JFormattedTextField();
        policyValidityConditionNotAfterDateText = new javax.swing.JFormattedTextField();
        policyValidityConditionNotAfterTimeText = new javax.swing.JFormattedTextField();
        policyIssuerText = new javax.swing.JTextField();
        x509IssuerNameLabel = new javax.swing.JLabel();
        x509SerialNumberLabel = new javax.swing.JLabel();
        x509SerialNumberText = new javax.swing.JTextField();
        x509IssuerNameText = new javax.swing.JTextField();

        centerPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        centerTitlePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("IoT@Work - Capability Based Authorization"));

        welcomeLabel.setText("Create Access Capability");

        policyIssuerLabel.setText("Issuer");

        policyAssigneeLabel.setText("Assignee *");

        policyResourceIDLabel.setText("Resource ID");

        policyAssigneeText.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));
        policyAssigneeText.setMinimumSize(new java.awt.Dimension(6, 20));

        policyResourceIDText.setEditable(false);

        policyRevocationSrvLabel.setText("Revocation Service");

        policyAccessRightsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Access Rights"));

        policyAccessRightsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {},
            new String [] {
                "Authorized Action Name", "Authorize", "Delegate"
            }
        )
        {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Boolean.class, java.lang.Boolean.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
            public boolean isCellEditable(int row, int column) {
                boolean delegate = (Boolean)this.getValueAt(row, 2);
                return (column == 2) || (column == 1 & !delegate);
            }
        });
        policyAccessRightsTable.setCellSelectionEnabled(true);
        jScrollPane1.setViewportView(policyAccessRightsTable);

        javax.swing.GroupLayout policyAccessRightsPanelLayout = new javax.swing.GroupLayout(policyAccessRightsPanel);
        policyAccessRightsPanel.setLayout(policyAccessRightsPanelLayout);
        policyAccessRightsPanelLayout.setHorizontalGroup(
            policyAccessRightsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(policyAccessRightsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 594, Short.MAX_VALUE)
                .addContainerGap())
        );
        policyAccessRightsPanelLayout.setVerticalGroup(
            policyAccessRightsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(policyAccessRightsPanelLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        policyValidityConditionPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Validity Condition"));

        poliocyValidityConditionNotBeforeLabel.setText("Not Before *");

        poliocyValidityConditionNotAfterLabel.setText("Not After *");

        poliocyValidityConditionNotBeforeDateButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icons_16x16/Calendar_16x16.png"))); // NOI18N
        poliocyValidityConditionNotBeforeDateButton.setText("Select");
        poliocyValidityConditionNotBeforeDateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                poliocyValidityConditionNotBeforeDateButtonActionPerformed(evt);
            }
        });

        poliocyValidityConditionNotAfterDateButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icons_16x16/Calendar_16x16.png"))); // NOI18N
        poliocyValidityConditionNotAfterDateButton.setText("Select");
        poliocyValidityConditionNotAfterDateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                poliocyValidityConditionNotAfterDateButtonActionPerformed(evt);
            }
        });

        policyValidityConditionNotBeforeDateText.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));
        policyValidityConditionNotBeforeDateText.setEditable(false);
        policyValidityConditionNotBeforeDateText.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(java.text.DateFormat.getDateInstance(java.text.DateFormat.LONG))));
        policyValidityConditionNotBeforeDateText.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        policyValidityConditionNotBeforeTimeText.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));
        policyValidityConditionNotBeforeTimeText.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("HH:mm:ss.SSS Z"))));
        policyValidityConditionNotBeforeTimeText.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        policyValidityConditionNotAfterDateText.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));
        policyValidityConditionNotAfterDateText.setEditable(false);
        policyValidityConditionNotAfterDateText.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(java.text.DateFormat.getDateInstance(java.text.DateFormat.LONG))));
        policyValidityConditionNotAfterDateText.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        policyValidityConditionNotAfterTimeText.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));
        policyValidityConditionNotAfterTimeText.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("HH:mm:ss.SSS Z"))));
        policyValidityConditionNotAfterTimeText.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        javax.swing.GroupLayout policyValidityConditionPanelLayout = new javax.swing.GroupLayout(policyValidityConditionPanel);
        policyValidityConditionPanel.setLayout(policyValidityConditionPanelLayout);
        policyValidityConditionPanelLayout.setHorizontalGroup(
            policyValidityConditionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(policyValidityConditionPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(policyValidityConditionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(poliocyValidityConditionNotBeforeLabel)
                    .addComponent(poliocyValidityConditionNotAfterLabel))
                .addGap(18, 18, 18)
                .addGroup(policyValidityConditionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(policyValidityConditionNotAfterDateText, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
                    .addComponent(policyValidityConditionNotBeforeDateText, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(policyValidityConditionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(poliocyValidityConditionNotBeforeDateButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(poliocyValidityConditionNotAfterDateButton))
                .addGap(18, 18, 18)
                .addGroup(policyValidityConditionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(policyValidityConditionNotAfterTimeText)
                    .addComponent(policyValidityConditionNotBeforeTimeText, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE))
                .addContainerGap())
        );
        policyValidityConditionPanelLayout.setVerticalGroup(
            policyValidityConditionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(policyValidityConditionPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(policyValidityConditionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(poliocyValidityConditionNotBeforeLabel)
                    .addComponent(poliocyValidityConditionNotBeforeDateButton)
                    .addComponent(policyValidityConditionNotBeforeDateText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(policyValidityConditionNotBeforeTimeText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(policyValidityConditionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(poliocyValidityConditionNotAfterLabel)
                    .addComponent(poliocyValidityConditionNotAfterDateButton)
                    .addComponent(policyValidityConditionNotAfterDateText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(policyValidityConditionNotAfterTimeText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        policyIssuerText.setEditable(false);

        x509IssuerNameLabel.setText("X509 Issuer Name");
        x509IssuerNameLabel.setToolTipText("The name of Issuer of the X509 certificate, as reported into the certificate itself.");

        x509SerialNumberLabel.setText("X509 Serial Number");
        x509SerialNumberLabel.setToolTipText("The Serial Number of the X509 certificate, as reported into the certificate itself.");

        x509SerialNumberText.setToolTipText("The Serial Number of the X509 certificate, as reported into the certificate itself.");

        x509IssuerNameText.setToolTipText("The name of Issuer of the X509 certificate, as reported into the certificate itself.");

        javax.swing.GroupLayout centerTitlePanelLayout = new javax.swing.GroupLayout(centerTitlePanel);
        centerTitlePanel.setLayout(centerTitlePanelLayout);
        centerTitlePanelLayout.setHorizontalGroup(
            centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(centerTitlePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(welcomeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 630, Short.MAX_VALUE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 630, Short.MAX_VALUE)
                    .addGroup(centerTitlePanelLayout.createSequentialGroup()
                        .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(x509SerialNumberLabel)
                            .addComponent(x509IssuerNameLabel)
                            .addComponent(policyIssuerLabel)
                            .addComponent(policyAssigneeLabel)
                            .addComponent(policyRevocationSrvLabel)
                            .addComponent(policyResourceIDLabel))
                        .addGap(18, 18, 18)
                        .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(policyResourceIDText, javax.swing.GroupLayout.DEFAULT_SIZE, 519, Short.MAX_VALUE)
                            .addComponent(policyRevocationSrvComboBox, 0, 519, Short.MAX_VALUE)
                            .addComponent(policyAssigneeText, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 519, Short.MAX_VALUE)
                            .addComponent(x509SerialNumberText, javax.swing.GroupLayout.DEFAULT_SIZE, 519, Short.MAX_VALUE)
                            .addComponent(policyIssuerText, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 519, Short.MAX_VALUE)
                            .addComponent(x509IssuerNameText, javax.swing.GroupLayout.DEFAULT_SIZE, 519, Short.MAX_VALUE)))
                    .addComponent(policyAccessRightsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(policyValidityConditionPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        centerTitlePanelLayout.setVerticalGroup(
            centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(centerTitlePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(welcomeLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(policyIssuerLabel)
                    .addComponent(policyIssuerText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(policyAssigneeLabel)
                    .addGroup(centerTitlePanelLayout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(policyAssigneeText, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)))
                .addGap(18, 18, 18)
                .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(x509IssuerNameLabel)
                    .addComponent(x509IssuerNameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(x509SerialNumberLabel)
                    .addComponent(x509SerialNumberText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(policyRevocationSrvLabel)
                    .addComponent(policyRevocationSrvComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(policyResourceIDLabel)
                    .addComponent(policyResourceIDText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(policyAccessRightsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(policyValidityConditionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout centerPanelLayout = new javax.swing.GroupLayout(centerPanel);
        centerPanel.setLayout(centerPanelLayout);
        centerPanelLayout.setHorizontalGroup(
            centerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(centerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(centerTitlePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        centerPanelLayout.setVerticalGroup(
            centerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(centerPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(centerTitlePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(centerPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(centerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void poliocyValidityConditionNotBeforeDateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_poliocyValidityConditionNotBeforeDateButtonActionPerformed
        handlePoliocyValidityConditionNotBeforeDateButtonActionPerformed();
    }//GEN-LAST:event_poliocyValidityConditionNotBeforeDateButtonActionPerformed

    private void poliocyValidityConditionNotAfterDateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_poliocyValidityConditionNotAfterDateButtonActionPerformed
        handlePoliocyValidityConditionNotAfterDateButtonActionPerformed();
    }//GEN-LAST:event_poliocyValidityConditionNotAfterDateButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel centerPanel;
    private javax.swing.JPanel centerTitlePanel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JPanel policyAccessRightsPanel;
    private javax.swing.JTable policyAccessRightsTable;
    private javax.swing.JLabel policyAssigneeLabel;
    private javax.swing.JTextField policyAssigneeText;
    private javax.swing.JLabel policyIssuerLabel;
    private javax.swing.JTextField policyIssuerText;
    private javax.swing.JLabel policyResourceIDLabel;
    private javax.swing.JTextField policyResourceIDText;
    private javax.swing.JComboBox policyRevocationSrvComboBox;
    private javax.swing.JLabel policyRevocationSrvLabel;
    private javax.swing.JFormattedTextField policyValidityConditionNotAfterDateText;
    private javax.swing.JFormattedTextField policyValidityConditionNotAfterTimeText;
    private javax.swing.JFormattedTextField policyValidityConditionNotBeforeDateText;
    private javax.swing.JFormattedTextField policyValidityConditionNotBeforeTimeText;
    private javax.swing.JPanel policyValidityConditionPanel;
    private javax.swing.JButton poliocyValidityConditionNotAfterDateButton;
    private javax.swing.JLabel poliocyValidityConditionNotAfterLabel;
    private javax.swing.JButton poliocyValidityConditionNotBeforeDateButton;
    private javax.swing.JLabel poliocyValidityConditionNotBeforeLabel;
    private javax.swing.JLabel welcomeLabel;
    private javax.swing.JLabel x509IssuerNameLabel;
    private javax.swing.JTextField x509IssuerNameText;
    private javax.swing.JLabel x509SerialNumberLabel;
    private javax.swing.JTextField x509SerialNumberText;
    // End of variables declaration//GEN-END:variables

    
    private void handlePoliocyValidityConditionNotBeforeDateButtonActionPerformed() {
        notBeforeDateDialog.setLocationRelativeTo(myContainer);
        notBeforeDateDialog.setVisible(true);
        if (notBeforeDateDialog.getReturnStatus() == DateChooserDialog.RET_OK) {
            policyValidityConditionNotBeforeDateText.setValue(notBeforeDateDialog.getSelectedDate());
            policyValidityConditionNotBeforeTimeText.setValue(notBeforeDateDialog.getSelectedDate());
            notAfterDateDialog.setMinSelectableDate(notBeforeDateDialog.getSelectedDate());
        }
    }                                                                           

    private void handlePoliocyValidityConditionNotAfterDateButtonActionPerformed() {
        notAfterDateDialog.setLocationRelativeTo(myContainer);
        notAfterDateDialog.setVisible(true);
        if (notAfterDateDialog.getReturnStatus() == DateChooserDialog.RET_OK) {
            policyValidityConditionNotAfterDateText.setValue(notAfterDateDialog.getSelectedDate());
            policyValidityConditionNotAfterTimeText.setValue(notAfterDateDialog.getSelectedDate());
            notBeforeDateDialog.setMaxSelectableDate(notAfterDateDialog.getSelectedDate());
        }
    }

    private void checkAssigneeDocument() {

        policyAssigneeText.getDocument().addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                sorroundIssuerDocumentChange(e);
            }

            public void removeUpdate(DocumentEvent e) {
                sorroundIssuerDocumentChange(e);
            }

            public void changedUpdate(DocumentEvent e) {
                sorroundIssuerDocumentChange(e);
            }

            private void sorroundIssuerDocumentChange(DocumentEvent e) {
                String strIssuer = policyAssigneeText.getText();
                if (!strIssuer.equals("")) {
                    policyAssigneeText.setBorder(
                            javax.swing.BorderFactory.createLineBorder(
                            new java.awt.Color(0, 0, 0)));
                } else {
                    policyAssigneeText.setBorder(
                            javax.swing.BorderFactory.createLineBorder(
                            new java.awt.Color(255, 0, 0)));
                }
            }
        });
    }
    
    private void checkNotBeforeDateDocument() {

        policyValidityConditionNotBeforeDateText.getDocument().addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                changeNotAfterDateBorderColor(e);
            }

            public void removeUpdate(DocumentEvent e) {
                changeNotAfterDateBorderColor(e);
            }

            public void changedUpdate(DocumentEvent e) {
                changeNotAfterDateBorderColor(e);
            }

            private void changeNotAfterDateBorderColor(DocumentEvent e) {
                if (policyValidityConditionNotBeforeDateText.getValue() != null) {
                    policyValidityConditionNotBeforeDateText.setBorder(
                            javax.swing.BorderFactory.createLineBorder(
                            new java.awt.Color(0, 0, 0)));
                } else {
                    policyValidityConditionNotBeforeDateText.setBorder(
                            javax.swing.BorderFactory.createLineBorder(
                            new java.awt.Color(255, 0, 0)));
                }

            }
        });
    }

    private void checkNotBeforeTimeDocument() {

        policyValidityConditionNotBeforeTimeText.getDocument().addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                changeNotAfterTimeBorderColor(e);
            }

            public void removeUpdate(DocumentEvent e) {
                changeNotAfterTimeBorderColor(e);
            }

            public void changedUpdate(DocumentEvent e) {
                changeNotAfterTimeBorderColor(e);
            }

            private void changeNotAfterTimeBorderColor(DocumentEvent e) {
                if (policyValidityConditionNotBeforeTimeText.getValue() != null) {
                    policyValidityConditionNotBeforeTimeText.setBorder(
                            javax.swing.BorderFactory.createLineBorder(
                            new java.awt.Color(0, 0, 0)));
                } else {
                    policyValidityConditionNotBeforeTimeText.setBorder(
                            javax.swing.BorderFactory.createLineBorder(
                            new java.awt.Color(255, 0, 0)));
                }

            }
        });
    }

    private void checkNotAfterDateDocument() {

        policyValidityConditionNotAfterDateText.getDocument().addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                changeNotAfterDateBorderColor(e);
            }

            public void removeUpdate(DocumentEvent e) {
                changeNotAfterDateBorderColor(e);
            }

            public void changedUpdate(DocumentEvent e) {
                changeNotAfterDateBorderColor(e);
            }

            private void changeNotAfterDateBorderColor(DocumentEvent e) {
                if (policyValidityConditionNotAfterDateText.getValue() != null) {
                    policyValidityConditionNotAfterDateText.setBorder(
                            javax.swing.BorderFactory.createLineBorder(
                            new java.awt.Color(0, 0, 0)));
                } else {
                    policyValidityConditionNotAfterDateText.setBorder(
                            javax.swing.BorderFactory.createLineBorder(
                            new java.awt.Color(255, 0, 0)));
                }

            }
        });
    }

    private void checkNotAfterTimeDocument() {

        policyValidityConditionNotAfterTimeText.getDocument().addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                changeNotAfterTimeBorderColor(e);
            }

            public void removeUpdate(DocumentEvent e) {
                changeNotAfterTimeBorderColor(e);
            }

            public void changedUpdate(DocumentEvent e) {
                changeNotAfterTimeBorderColor(e);
            }

            private void changeNotAfterTimeBorderColor(DocumentEvent e) {
                if (policyValidityConditionNotAfterTimeText.getValue() != null) {
                    policyValidityConditionNotAfterTimeText.setBorder(
                            javax.swing.BorderFactory.createLineBorder(
                            new java.awt.Color(0, 0, 0)));
                } else {
                    policyValidityConditionNotAfterTimeText.setBorder(
                            javax.swing.BorderFactory.createLineBorder(
                            new java.awt.Color(255, 0, 0)));
                }

            }
        });
    }

    private void checkDelegateToAuthorize() {

        policyAccessRightsTable.getModel().addTableModelListener(new TableModelListener() {

            public void tableChanged(TableModelEvent e) {

                if ((e.getType() == TableModelEvent.UPDATE) && e.getColumn() == 2) {
                    int row = e.getFirstRow();
                    boolean delegate = (Boolean) policyAccessRightsTable.getModel().getValueAt(row, 2);
                    if (delegate) {
                        policyAccessRightsTable.getModel().setValueAt(true, row, 1);
                    }
                }
            }
        });
    }

    public boolean checkFormValidity() throws PanelAccessTypeCapabilityException {

        if (policyAssigneeText.getText().equals("")) {
            throw new PanelAccessTypeCapabilityException("The Assigee value is empty!");
        }

        if (policyValidityConditionNotBeforeDateText.getValue() == null) {
            throw new PanelAccessTypeCapabilityException("The validity condition - not before date is empty or invalid!");
        }

        if (policyValidityConditionNotBeforeTimeText.getValue() == null) {
            throw new PanelAccessTypeCapabilityException("The validity condition - not before time is empty or invalid!");
        }

        if (policyValidityConditionNotAfterDateText.getValue() == null) {
            throw new PanelAccessTypeCapabilityException("The validity condition - not after date is empty or invalid!");
        }

        if (policyValidityConditionNotAfterTimeText.getValue() == null) {
            throw new PanelAccessTypeCapabilityException("The validity condition - not after time is empty or invalid!");
        }
        return true;
    }

    public void setIssuerText(String issuer){
        policyIssuerText.setText(issuer);
    }

    public void setPolicyRevocationServices(List<String> services) {
        policyRevocationSrvComboBox.removeAllItems();
        for (String service : services) {
            policyRevocationSrvComboBox.addItem(service);
        }
    }

    public void setPolicyResourceIDText(String policyResourceIDStr) {
        policyResourceIDText.setText(policyResourceIDStr);
    }
    
    public void resetCapabilityActions() {
        DefaultTableModel model = (DefaultTableModel) policyAccessRightsTable.getModel();
        while (model.getRowCount() > 0) {
            model.removeRow(0);
        }
    }

    public void addCapabilityAction(CapabilityAction capabilityAction) {
        DefaultTableModel model = (DefaultTableModel) policyAccessRightsTable.getModel();
        Object accesRight[] = new Object[3];
        accesRight[0] = capabilityAction.getValue();
        accesRight[1] = capabilityAction.isAuthorize();
        accesRight[2] = capabilityAction.isDelegable();
        model.addRow(accesRight);
    }

    public String getAssigneeText(){
        return policyAssigneeText.getText();
    }
    
    public CapabilityAction[] getCapabilityActions() {
        DefaultTableModel model = (DefaultTableModel) policyAccessRightsTable.getModel();
        int rowCount = model.getRowCount();
        CapabilityAction[] actions = new CapabilityAction[rowCount];
        for (int row = 0; row < rowCount; row++) {
            CapabilityAction action = new CapabilityAction();
            action.setValue((String) model.getValueAt(row, 0));
            action.setAuthorize((Boolean) model.getValueAt(row, 1));
            action.setDelegable((Boolean) model.getValueAt(row, 2));
            actions[row] = action;
        }
        return actions;
    }

    public Date getNotBeforeDate() {
        return (Date)policyValidityConditionNotBeforeDateText.getValue();
    }

    public Date getNotBeforeTime() {
        return (Date)policyValidityConditionNotBeforeTimeText.getValue();
    }

    public Date getNotOrOnAfterDate() {
        return (Date)policyValidityConditionNotAfterDateText.getValue();
    }

    public Date getNotOrOnAfterTime() {
        return (Date)policyValidityConditionNotAfterTimeText.getValue();
    }

    public String getX509IssuerName() {
        return x509IssuerNameText.getText();
    }

    public String getX509SerialNumber() {
        return x509SerialNumberText.getText();
    }

    public void setMinSelectableDate(Date minSelectableDate) {
        notBeforeDateDialog.setMinSelectableDate(minSelectableDate);
        notAfterDateDialog.setMinSelectableDate(minSelectableDate);
    }

    public void setMaxSelectableDate(Date maxSelectableDate) {
        notBeforeDateDialog.setMaxSelectableDate(maxSelectableDate);
        notAfterDateDialog.setMaxSelectableDate(maxSelectableDate);
    }
}
